//
//  HomeDynamicCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/27.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "HomeDynamicCell.h"

@interface HomeDynamicCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) HomeDynamicBtnView *tranView;
@property (nonatomic, strong) HomeDynamicBtnView *storageView;
@property (nonatomic, strong) HomeDynamicBtnView *packageView;
@property (nonatomic, strong) UIView *mapView;

@end

@implementation HomeDynamicCell

+ (instancetype)HomeDynamicCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"HomeDynamicCell";
    HomeDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HomeDynamicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 20, 197)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        CGFloat viewW = (self.bgView.nim_width - 10) * 0.33333;
        CGFloat viewH = 40;
        self.tranView = [[HomeDynamicBtnView alloc] initWithFrame:CGRectMake(5, 6, viewW, viewH)];
        self.tranView.titleStr = @"运输动态";
        self.tranView.isSelected = YES;
        __weak_self;
        self.tranView.clickBlock = ^{
            weakself.tranView.isSelected = YES;
            weakself.storageView.isSelected = NO;
            weakself.packageView.isSelected = NO;
        };
        [self.bgView addSubview:self.tranView];
        
        self.storageView = [[HomeDynamicBtnView alloc] initWithFrame:CGRectMake(self.tranView.nim_right, 6, viewW, viewH)];
        self.storageView.titleStr = @"仓储动态";
        self.storageView.isSelected = NO;
        self.storageView.clickBlock = ^{
            weakself.tranView.isSelected = NO;
            weakself.storageView.isSelected = YES;
            weakself.packageView.isSelected = NO;
        };
        [self.bgView addSubview:self.storageView];
        
        self.packageView = [[HomeDynamicBtnView alloc] initWithFrame:CGRectMake(self.storageView.nim_right, 6, viewW, viewH)];
        self.packageView.titleStr = @"包装动态";
        self.packageView.isSelected = NO;
        self.packageView.clickBlock = ^{
            weakself.tranView.isSelected = NO;
            weakself.storageView.isSelected = NO;
            weakself.packageView.isSelected = YES;
        };
        [self.bgView addSubview:self.packageView];
        
        self.mapView = [[UIView alloc] initWithFrame:CGRectMake(5, self.tranView.nim_bottom + 6, self.bgView.nim_width - 10, 140)];
        self.mapView.backgroundColor = Color_Separator;
        self.mapView.layer.masksToBounds = YES;
        self.mapView.layer.cornerRadius = 6;
        [self.bgView addSubview:self.mapView];
        
        self.height = self.bgView.nim_bottom;
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface HomeDynamicBtnView ()

@property (nonatomic, strong) UIButton *clickBtn;
@property (nonatomic, strong) UILabel *line;

@end

@implementation HomeDynamicBtnView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.clickBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.clickBtn.titleLabel.font = BoldFontOfSize(16);
        [self.clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.clickBtn];
        
        self.line = [[UILabel alloc] initWithFrame:CGRectMake((frame.size.width - 12) * 0.5, frame.size.height - 3, 12, 3)];
        self.line.layer.masksToBounds = YES;
        self.line.layer.cornerRadius = 1.5;
        [self addSubview:self.line];
    }
    return self;
}

- (void)btnClick {
    if (self.clickBlock) {
        self.clickBlock();
    }
}

-(void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    if (isSelected) {
        [self.clickBtn setTitleColor:Color_ThemeColor forState:0];
        self.line.backgroundColor = Color_ThemeColor;
    }else {
        [self.clickBtn setTitleColor:HexColor(0x585974) forState:0];
        self.line.backgroundColor = HexColor(0xffffff);
    }
}

-(void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    [self.clickBtn setTitle:titleStr forState:0];
}

@end
